#!/bin/sh
#
# see: dh_installdeb(1)

set -e

package="bone101"

npm_pkg_install () {
	echo "${package}:installing:[${npm_file}]"
	if [ ! -d /usr/local/lib/node_modules/ ] ; then
		mkdir -p /usr/local/lib/node_modules/ || true
	fi
	if [ -d /usr/local/lib/node_modules/${npm_project}/ ] ; then
		rm -rf /usr/local/lib/node_modules/${npm_project}/ || true
	fi

	echo "${package}:updating:[/usr/local/lib/node_modules/${npm_project}/]"
	tar xf /var/cache/${package}/${npm_file} -C /usr/local/lib/node_modules/
}

npm_install () {
	node_bin="/usr/bin/nodejs"

	unset node_version
	node_version=$(/usr/bin/nodejs --version || true)
	echo "${package}:nodejs:[${node_version}]"

	v8="v8.10.0"

	node_async="async-2.0.0-rc.6"
	node_sensortag="sensortag-1.2.3"

	case "${node_version}" in
	v8.*)
		npm_project="async"
		npm_file="${node_async}-${v8}.tar.xz"
		npm_pkg_install
		npm_project="sensortag"
		npm_file="${node_sensortag}-${v8}.tar.xz"
		npm_pkg_install
		;;
	esac
}

npm_install
addgroup cloud9ide --system || true

echo "Setting up: /var/lib/cloud9/ permissions"

chown -R root:cloud9ide /var/lib/cloud9/ || true
chmod -R 775 /var/lib/cloud9/ || true
if [ -d /var/lib/cloud9/examples/ ] ; then
	cd /var/lib/cloud9/examples/
	git init .
	git config user.email "beagle@beagleboard.org"
	git config user.name "BeagleBoard"
	git add . -f
	git commit -a -m 'inital repo' --author="BeagleBoard <beagle@beagleboard.org>" --quiet
fi

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0

